/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { TextEncoder, TextDecoder } from "./textEncoding.js";
import { str } from './string.js'

let count=5000;
let count1=200;
function TextEncoder_test(){
    let Encoder;
    let start=Date.now()
    for(let i=0;i<count;i++){
      Encoder=new TextEncoder('utf-8')
    }
    let end=Date.now()
    let encode = Encoder.encode(str);
    // print(encode)
    print(`textEncoding_TextEncoder: ${end-start} ms`)
    if(encode[0]!==84){
        throw new Error('textEncoding_TextEncoder err')
    }
}

function encode_test() {
    let Encoder=new TextEncoder('utf-8')
    let encode
    let start=Date.now()
    for(let i=0;i<count1;i++){
        encode = Encoder.encode(str);
    }
    let end=Date.now()
    let deco = new TextDecoder('utf-8').decode(encode);
    print(`textEncoding_encode('utf-8'): ${end-start} ms`)
    if(str!==deco){
        throw new Error('textEncoding_encode err')
    }
}

function encode_test_01() {
    let Encoder=new TextEncoder('utf-16le')
    let str=new Uint8Array([0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xDB, 0xFD, 0xDF])
//    let str=encode
    let encode
    let start=Date.now()
    for(let i=0;i<count1;i++){
        encode = Encoder.encode(str);
    }
    let end=Date.now()
    let deco = new TextDecoder('utf-16le').decode(encode);
    print(`textEncoding_encode('utf-16le'): ${end-start} ms`)
    // print(deco)
    if('㈱ⰲⰰ㘱ⰲⰰ㈵ㄬ㠰㔬ⰲㄲⰶ〳㈬ㄲ㈬㔵㈬㤱㈬㌵㈬㌲'!==deco){
        throw new Error('textEncoding_encode err')
    }
}
function encode_test_02() {
    let Encoder=new TextEncoder('utf-16be')
    let str="z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD\u306B\u307B\u3093\x00123ABCabc\x80\xFF\u0100\u1000\uFFFD\uD800\uDC00\uDBFF\uDFFF"
    let encode
    let start=Date.now()
    for(let i=0;i<count1;i++){
        encode = Encoder.encode(str);
    }
    let end=Date.now()
    let deco = new TextDecoder('utf-16be').decode(encode);
    print(`textEncoding_encode('utf-16be'): ${end-start} ms`)
    // print(deco)
    if('竂ꋦ내蒞뾽ꯣ膻錀ㄲ㍁䉃慢揂胃뿄胡肀뷰邀胴辿�'!==deco){
        throw new Error('textEncoding_encode err')
    }
}

function TextDecoder_tset(){
    let encode = new TextEncoder('utf-8').encode(str);
    let Decoder;
    let start=Date.now()
    for(let i=0;i<count;i++){
        Decoder=new TextDecoder('utf-8')
    }
    let end=Date.now()
    let deco = Decoder.decode(encode);
    // print(deco)
    print(`textEncoding_TextDecoder: ${end-start} ms`)
    if(str!==deco){
        throw new Error('textEncoding_encode err')
    }
}


function decode_test() {
    let encode = new TextEncoder('utf-8').encode(str);
    let deco;
    let Decoder=new TextDecoder('utf-8');
    let start=Date.now()
    for(let i=0;i<count1;i++){
        deco = Decoder.decode(encode);
    }
    let end=Date.now()
    let encode1 = new TextEncoder('utf-8').encode(deco);
    // print(encode1)
    print(`textEncoding_decode('utf-8'): ${end-start} ms`)
    if(JSON.stringify(encode)!==JSON.stringify(encode1)){
        throw new Error('textEncoding_decode err')
    }

}
function decode_test_01() {
    let Encoder=new TextEncoder('utf-16le')
    let str=new Uint8Array([0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xDB, 0xFD, 0xDF])
    let encode=Encoder.encode(str);
    let deco;
    let start=Date.now()
    for(let i=0;i<count1;i++){
        deco = new TextDecoder('utf-16le').decode(encode);
    }
    let end=Date.now()
    print(`textEncoding_decode('utf-16le'): ${end-start} ms`)
    // print(deco)
    if('㈱ⰲⰰ㘱ⰲⰰ㈵ㄬ㠰㔬ⰲㄲⰶ〳㈬ㄲ㈬㔵㈬㤱㈬㌵㈬㌲'!==deco){
        throw new Error('textEncoding_decode err')
    }
}
function decode_test_02() { 
    let str="z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD\u306B\u307B\u3093\x00123ABCabc\x80\xFF\u0100\u1000\uFFFD\uD800\uDC00\uDBFF\uDFFF"
    let Encoder=new TextEncoder('utf-16be').encode(str)
    let code=new TextDecoder('utf-16be')
    let deco;
    let start=Date.now()
    for(let i=0;i<count1;i++){
        deco = code.decode(Encoder);
    }
    let end=Date.now()
    print(`textEncoding_decode('utf-16be'): ${end-start} ms`)
    // print(deco)
    if('竂ꋦ내蒞뾽ꯣ膻錀ㄲ㍁䉃慢揂胃뿄胡肀뷰邀胴辿�'!==deco){
        throw new Error('textEncoding_decode err')
    }
}

let loopCountForPreheat = 1;
for (let i = 0; i < loopCountForPreheat; i++) {
    TextEncoder_test()
    encode_test()
    encode_test_01()
    encode_test_02()
    TextDecoder_tset()
    decode_test()
    decode_test_01()
    decode_test_02()
}

ArkTools.waitAllJitCompileFinish();

TextEncoder_test()
encode_test()
encode_test_01()
encode_test_02()
TextDecoder_tset()
decode_test()
decode_test_01() 
decode_test_02() 


